<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    *{
      margin: 0;
      padding: 0;
    }
    .box1{
      width: 100px;
      height: 100px;
      background-color: red;
      position: relative;
    }
    .box2{
      width: 100px;
      height: 100px;
      background-color: blue;
      position: relative;
    }
  </style>
</head>
<body>
  <input type="button" value="移动">
  <div class="box1"></div>
  <div class="box2"></div>
  <script>
    var btn = document.querySelector('input');
    var box1 = document.querySelector('.box1');
    var box2 = document.querySelector('.box2');
    btn.onclick = function(){
      animate(box1,400,10);
      animate(box2,800,5);
    };
    var timeID = null;
    function animate(element,target,step){
      if(element.timeID){
        clearInterval(element.timeID);
        element.timeID = null;
      };
      element.timeID = setInterval(function(){
        var current = element.offsetLeft;
        if(current >= target){
          current = target;
          clearInterval(element.timeID);
        }else{
          current += step;
        };
        element.style.left = current + 'px';
      },30);
    };
  </script>
</body>
</html>